/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.common  
 */
package com.apache.bid.common;

import java.math.BigDecimal;

import org.apache.log4j.Logger;

import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.entity.BidApply;
import com.apache.bid.entity.BidInfoBaojia;
import com.apache.bid.entity.BidItemVip;
import com.apache.bid.entity.BidSession;
import com.apache.tools.StrUtil;
import com.apache.uct.common.ToolsUtil;

/**
 * description:   报价验证工具类
 * @author Hou Dayu 创建时间：2016-3-29  
 * 
 */
public class BidValidatorUtil {

	private Logger log = Logger.getLogger(getClass());
	private static BidValidatorUtil instance;

	private BidValidatorUtil() {
	}

	public static BidValidatorUtil getInstance() {
		if (null == instance) {
			instance = new BidValidatorUtil();
		}
		return instance;
	}

	/**
	 * description:  验证apply资格
	 */
	@SuppressWarnings("rawtypes")
	public String checkApply(String seType, String seId, String itemId, String userEname, String firstValue) {
		String key = "";
		if (Constant.SETYPE_HD.equals(seType)) {
			key = BidCacheManager.apply_prefix + userEname + "_" + itemId;
		} else {
			key = BidCacheManager.apply_prefix + userEname + "_" + seId;
		}
		BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("apply");
		BidApply apply = (BidApply) cache.getCacheObjectByKey(key);
		if (ToolsUtil.isEmpty(apply))
			return "您还没有申请报价";
		if ("0".equals(apply.getStatus()))
			return "您的资格还没有被激活";
		if (!Constant.SETYPE_HD.equals(seType)) {//如果是活动则不处理
			BidSession session = (BidSession) cache.getCacheObjectByKey(cache.session_prefix + seId);
			if (Constant.BIDMODE_FEJJZX.equals(session.getSessionMode())
					|| Constant.BIDMODE_FEJJFX.equals(session.getSessionMode())) {
			} else {
				return "";
			}
			BigDecimal fact_bail = new BigDecimal(StrUtil.doNull(apply.getFactBail(), "0"));
			BigDecimal bail_max = new BigDecimal(StrUtil.doNull(session.getBailMax(), "1"));
			if (fact_bail.compareTo(bail_max) >= 0 && new BigDecimal(1).compareTo(bail_max) < 0) {//等于封顶会值时不处理
				return "";
			}
			if (Constant.BAILCTRL_NUM.equals(session.getBailCtrl())) {//按数量处理标的数量
				String keyPrefix = BidCacheManager.bailCtrlPrice_prefix + seId + "_" + apply.getUserName();
				if (cache.checkCacheObject(keyPrefix)) {
					long count = Long.parseLong(String.valueOf(cache.getCacheObjectByKey(keyPrefix)));
					long allowBuyNum = Long.parseLong(StrUtil.doNull(apply.getAllowBuyNum(), "1"));
					if (count + 1 > allowBuyNum) {//大于允许购买数量时不处理
						return "您已达到允许购买的标的数量";
					}
				}
			} else if (Constant.BAILCTRL_BL.equals(session.getBailCtrl())) {
				String keyPrefix = BidCacheManager.bailCtrlPrice_prefix + seId + "_" + apply.getUserName();
				if (cache.checkCacheObject(keyPrefix)) {
					BigDecimal count = new BigDecimal(String.valueOf(cache.getCacheObjectByKey(keyPrefix)));
					BigDecimal bail_rate = new BigDecimal(session.getBailRate());//小数如：0.1
					BigDecimal first_Value = new BigDecimal(firstValue);
					if ((first_Value.multiply(bail_rate).add(count)).compareTo(fact_bail) > 0) {//大于保证金不做处理
						return "您已超过交保证金所允许购买的标的数量";
					}
				}
			}
		}
		return "";
	}

	/**
	 * description:  验证优先权
	 */
	public String checkItemVip(String userEname, String itemId, String lastRightLevel) {
		String vipKey = BidCacheManager.item_vip_prefix + userEname + "_" + itemId;
		BidItemVip vip = (BidItemVip) BidCacheFactory.getInstance().getCacheManager("itemVip")
				.getCacheObjectByKey(vipKey);
		if (ToolsUtil.isEmpty(vip)) {
			return "您无权行使优先权";
		}
		int myLevel = Integer.valueOf(vip.getVipLevel());
		int nowLevel = Integer.valueOf(lastRightLevel);
		if (myLevel <= nowLevel) {
			return "您的优先权级别过低不能行使";
		}
		return "";
	}

	/**
	 * description:  验证一次报价
	 */
	@SuppressWarnings("rawtypes")
	public String checkBaojia(String userEname, String itemId, String allowUpdateTimes) {
		BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("baojia");
		BidInfoBaojia bjVo = (BidInfoBaojia) cache.getCacheCloneByKey(cache.baojia_prefix + userEname + "_" + itemId);
		if (ToolsUtil.isEmpty(bjVo))
			return "";
		if ("0".equals(allowUpdateTimes)) {//允许修改报价的次数（bidmode=3/4使用）（缺省=0不允许修改）
			return "您已报过价，不能修改报价信息";
		}
		int ifUpdateTimes = Integer.parseInt(allowUpdateTimes);
		int updateTimes = Integer.parseInt(StrUtil.doNull(bjVo.getUpdateTimes(), "0"));
		if (updateTimes >= ifUpdateTimes) {
			return "您已超过允许修改的次数，不能修改报价信息";
		}
		return "";
	}
}
